{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "closing-cylinder",
   "metadata": {},
   "outputs": [],
   "source": [
    "import librosa\n",
    "import librosa.display\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "from dtw import dtw\n",
    "from my_dtw.utils import *\n",
    "from scipy.io import wavfile"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "engaged-worth",
   "metadata": {},
   "outputs": [],
   "source": [
    "CHUNK = 512  # 每个缓冲区的帧数\n",
    "CHANNELS = 1  # 单声道\n",
    "RATE = 44100  # 采样频率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fundamental-command",
   "metadata": {},
   "outputs": [],
   "source": [
    "def score_mfcc(mfcc_1, mfcc_2):\n",
    "    return dtw(mfcc_1, mfcc_2, dist_method='euclidean')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "monetary-filename",
   "metadata": {},
   "outputs": [],
   "source": [
    "template = Voice(r'./recordings/1_1_.wav')\n",
    "me = Voice(r'./recordings/1_2_.wav')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 202,
   "id": "extensive-factor",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(22528,) 44100\n",
      "(28672,) 44100\n",
      "(13824,) 44100\n"
     ]
    }
   ],
   "source": [
    "template = librosa.load(r'./recordings/template_processed.wav', sr=None)\n",
    "print(template[0].shape, template[1])\n",
    "hei = librosa.load(r'./recordings/hei_processed.wav', sr=None)\n",
    "print(hei[0].shape, hei[1])\n",
    "zuoxia = librosa.load(r'./recordings/zuoxia_processed.wav', sr=None)\n",
    "print(zuoxia[0].shape, zuoxia[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "stuffed-mexico",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0.5, 1.0, 'Monophonic')"
      ]
     },
     "execution_count": 7,
     "metadata": {},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Y80xkSzX4BLvOgBPAGPMEsBj4DxFxA43A1SbahjGGKOqGZSqllOob0diko5RSqg9owFdKqRihAV8ppWKEBnyllIoRGvCVUipGaMBXChCRYbYsiYdF5ID1uk5E/hDp8ikVDjosU6l2ROR+oM4Y8+tIl0WpcNIavlKdsHKkv2m9vl9ElonIv0WkUEQuE5FfisgWEXlHRJzWdnNFZLWIbBCRd9s9taxUxGjAV6pnJgFn40s38RzwgTFmBr4nMy+0gv7vgMXGmLnAs8BDkSqsUnZRl1pBqSj3tjHGJSJb8KWleMdavgXIBaYC04FVVloWB3AoAuVU6iga8JXqmWYAY4xXRFy2XCtefH9PAmwzxsyPVAGV6og26SgVXjuAHBGZDyAizsE4kYYamDTgKxVG1pSRi4FHRWQTsBE4NaKFUsqiwzKVUipGaA1fKaVihAZ8pZSKERrwlVIqRmjAV0qpGKEBXymlYoQGfKWUihEa8JVSKkZowFdKqRjxfyld/C6CF/ajAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "template = librosa.load(r'./recordings/1_1.wav', sr=None)\n",
    "\n",
    "plt.figure()\n",
    "plt.subplot(3, 1, 1)\n",
    "librosa.display.waveplot(template[0], sr=RATE)\n",
    "plt.title('Monophonic')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "controlled-ability",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.8701782"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "living-isaac",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(20, 46)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<matplotlib.collections.QuadMesh at 0x7fb72248e7f0>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAWsAAAEGCAYAAACjLLT8AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8rg+JYAAAACXBIWXMAAAsTAAALEwEAmpwYAAAWnklEQVR4nO3de8xkd13H8c/3zDyXbWm7SymUUq4KRqoIdCFAvCAKIgRQIIEQAiT+I2CMMd5I+YNITIQgCQoGMVGLhnBLTIjchAqChkoAS2tpFrZYtFCKvUDZ7j6XmfPzj3Og083+Pr/ZM8888/zo+5VsMju/Oed853d+833OPrPf842UkgAAB1uz6gAAAGUkawCoAMkaACpAsgaACpCsAaAC42Xs9MIjF6SHXXJxdjwp9vyYocL/alnWf3oZ+lZsPCsItvA+lnHOusOa9zpwjoqRuv8BFcPf5/Cztpy5XcwS1mBpbt0h3aYr+SwtxzU3fO22lNJFZxpbSrJ+2CUX61/e867seGpG+bHIX+xHO82PpdbGFO4DWtjWce/Fce+lSfmxIvNe2tFGfrPCB8mdl0W482bnqN01+yz94Dbne4H36eYwxbA1X4xngbXr2PMyMAFOR+uFY+b36+bWbefWSbfj/Pt052wRYT7fFxx99jdyY/waBAAqQLIGgAqQrAGgAiRrAKgAyRoAKkCyBoAKkKwBoAIkawCoAMkaACqwlApGpaTRZCs7HNNJfmxZFVnTYRVvpQrFodWYTmNinSem7H4nO/l9FmJNo2FLpVRNaN+Lq8Zs1gbFIw1fY67ybJFtm2n+vNjSeMmXcLtzWpqDgWvXnc/SunaVke7zu9D7NPMbyucpDfwMdscctv64sgaACpCsAaACJGsAqADJGgAqQLIGgAqQrAGgAiRrAKgAyRoAKkCyBoAKkKwBoALLKTeP0HS8mR1uwpR+m5LTRTpsxzjfrNOVRLeNn6KhJeW25LnYBXpYd+6hZerdfoeV9JaO2ZqmpG5mR6ZEu9T817FNmc1YiSv1t1pfmpzG+bJ734i3tMZMqbURrrF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y/f0N+j+LMR8Y2IeGF09+i+LiI+FhFr/esuj4h/7e+v/PH+Pi3AgUCyxn3Fj6m71ezzJf2DpE+llH5a3Q2pntsn7L9QV2p/uaS/kfQnqwoWON198a57uG/6aEppNyKuU1dC/7H++evU3Vz+J9Td3OcT/Z38RupukwkcCCRr3FdsS1Lqbju7m+75sqZV9zkIdfdDeeqqAgQcfg0CdI6p68zzVOmHvfguW3FMwA+RrAFJqWvV9GJJb4qIL6u7C9zTVhoUMIP/ugcAFeDKGgAqQLIGgAqQrAGgAiRrAKgAyRoAKkCyBoAKkKwBoAL/D1giraQsj+Y0AAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "sr = 44100\n",
    "\n",
    "template_mfccs = librosa.feature.mfcc(template[0], sr=sr)\n",
    "print(template_mfccs.shape)\n",
    "\n",
    "#Displaying the MFCCs:\n",
    "librosa.display.specshow(template_mfccs, sr=sr, x_axis='time')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 212,
   "id": "transsexual-pencil",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(20, 45)\n",
      "(20, 57)\n",
      "(20, 28)\n"
     ]
    }
   ],
   "source": [
    "sr = 44100\n",
    "\n",
    "template_mfccs = librosa.feature.mfcc(template[0], sr=sr)\n",
    "print(template_mfccs.shape)\n",
    "\n",
    "hei_mfccs = librosa.feature.mfcc(hei[0], sr=sr)\n",
    "print(hei_mfccs.shape)\n",
    "\n",
    "zuoxia_mfccs = librosa.feature.mfcc(zuoxia[0], sr=sr)\n",
    "print(zuoxia_mfccs.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 205,
   "id": "regular-thread",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = dtw(hei_mfccs.T, template_mfccs.T, dist_method='euclidean')\n",
    "b = dtw(zuoxia_mfccs.T, template_mfccs.T, dist_method='euclidean')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 206,
   "id": "theoretical-complement",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4168.534450061933, 6465.938028357658)"
      ]
     },
     "execution_count": 206,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.distance, b.distance"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 207,
   "id": "hindu-frame",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(40.86798480452875, 88.574493539146)"
      ]
     },
     "execution_count": 207,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.normalizedDistance, b.normalizedDistance"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "false-orbit",
   "metadata": {},
   "source": [
    "# 端点检测-1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "promotional-lincoln",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 符号函数\n",
    "def sgn(n):\n",
    "    if n >=0 :\n",
    "        return 1\n",
    "    else:\n",
    "        return 0\n",
    "\n",
    "# 短时能量\n",
    "def cal_energy(wave_data, frame_size=1024):\n",
    "    sum = 0\n",
    "    energy = []\n",
    "    step = frame_size >> 1 \n",
    "    for i in range(len(wave_data)):\n",
    "#         sum += int(wave_data[i]**2)\n",
    "#         print(i, wave_data[i])\n",
    "        sum = sum + int(wave_data[i]) * int(wave_data[i])\n",
    "        if (i + 1) % step == 0:\n",
    "            energy.append(sum)\n",
    "            sum = 0\n",
    "        elif i == len(wave_data) - 1:\n",
    "            energy.append(sum)\n",
    "    return energy\n",
    "\n",
    "\n",
    "# 短时过零率\n",
    "def zero_cross_rate(wave_data, frame_size=1024):\n",
    "    zcr = []\n",
    "    sum = 0.0\n",
    "    N = len(wave_data)\n",
    "    _fs = frame_size - 1\n",
    "    \n",
    "    for i in range(N):\n",
    "        if i % frame_size == 0:\n",
    "            continue\n",
    "        sum += np.abs(sgn(wave_data[i]) - sgn(wave_data[i - 1]))\n",
    "        \n",
    "        if (i + 1) % frame_size == 0 or i == len(wave_data) - 1:\n",
    "            zcr.append(sum / 255)\n",
    "            sum = 0.0\n",
    "    return zcr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 213,
   "id": "sized-acoustic",
   "metadata": {},
   "outputs": [],
   "source": [
    "# audio point check\n",
    "def strip_silence(wave_data, frame_length=512, hop_length=256):\n",
    "    energies = librosa.feature.rms(y=wave_data, frame_length=frame_length, hop_length=hop_length)[0]\n",
    "    thres = 2 * np.median(energies)\n",
    "    keep_index = np.where(energies > thres)\n",
    "    new_signal_index = librosa.frames_to_samples(keep_index, hop_length=hop_length)[0]\n",
    "    new_signal = np.concatenate([wave_data[x: x + hop_length] for x in new_signal_index if x < len(wave_data)])\n",
    "    return new_signal"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 248,
   "id": "cloudy-cheese",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((1, 307),\n",
       " array([[0.01484891, 0.01869349, 0.01716645, 0.00835146, 0.00490072,\n",
       "         0.0039664 , 0.00308446, 0.00278975, 0.00427531, 0.007256  ,\n",
       "         0.00902174, 0.00798089, 0.00896921, 0.01255635, 0.01199964,\n",
       "         0.01573703, 0.01834616, 0.01482241, 0.01782421, 0.01808631,\n",
       "         0.01833151, 0.01676749, 0.01925392, 0.02138822, 0.01635909,\n",
       "         0.01780197, 0.01929312, 0.01694762, 0.01587857, 0.01524573,\n",
       "         0.01651851, 0.02245991, 0.02364388, 0.01895583, 0.01920386,\n",
       "         0.02174607, 0.02110161, 0.02606663, 0.0247869 , 0.02115367,\n",
       "         0.01984422, 0.02037754, 0.01828906, 0.01636884, 0.01910341,\n",
       "         0.01828574, 0.02388668, 0.02373165, 0.02110428, 0.02189882,\n",
       "         0.01621443, 0.01065934, 0.01408415, 0.01464633, 0.01584072,\n",
       "         0.016989  , 0.01657869, 0.01970397, 0.02585958, 0.02402519,\n",
       "         0.02236019, 0.03008761, 0.02665039, 0.02471797, 0.02806943,\n",
       "         0.02567775, 0.02402042, 0.01954461, 0.01392177, 0.01452813,\n",
       "         0.01515197, 0.01841727, 0.02188679, 0.02007964, 0.02219233,\n",
       "         0.01961468, 0.02141723, 0.0232799 , 0.01789511, 0.01969425,\n",
       "         0.02043316, 0.01844477, 0.02009934, 0.02148534, 0.0233367 ,\n",
       "         0.02701792, 0.02308228, 0.02394618, 0.02422237, 0.01993497,\n",
       "         0.01952756, 0.01400922, 0.01470954, 0.01527349, 0.01284979,\n",
       "         0.0178959 , 0.02041389, 0.01820094, 0.02081607, 0.02005741,\n",
       "         0.02144219, 0.02782321, 0.03200781, 0.0333923 , 0.03013586,\n",
       "         0.02554066, 0.02511512, 0.02368251, 0.02356309, 0.0225519 ,\n",
       "         0.02249303, 0.0272476 , 0.03542193, 0.10332657, 0.19381967,\n",
       "         0.2906415 , 0.31627214, 0.3276997 , 0.36776343, 0.34115005,\n",
       "         0.30060667, 0.37597656, 0.41029465, 0.35096166, 0.3253842 ,\n",
       "         0.3382015 , 0.30846184, 0.28634524, 0.3354397 , 0.35127163,\n",
       "         0.26274455, 0.21128811, 0.21799596, 0.16811794, 0.14660019,\n",
       "         0.11567245, 0.11567566, 0.12491918, 0.10027295, 0.0941402 ,\n",
       "         0.13036622, 0.14606102, 0.19940785, 0.28826004, 0.2823019 ,\n",
       "         0.25677726, 0.32934958, 0.31950808, 0.27786165, 0.345737  ,\n",
       "         0.34423697, 0.30633256, 0.3836697 , 0.3689567 , 0.31028622,\n",
       "         0.31600592, 0.24366696, 0.15189937, 0.13020498, 0.13494204,\n",
       "         0.11332425, 0.090601  , 0.09226906, 0.10889515, 0.1552797 ,\n",
       "         0.16686699, 0.16785626, 0.2139194 , 0.25771886, 0.24220346,\n",
       "         0.2312631 , 0.28624833, 0.27887127, 0.253559  , 0.3067096 ,\n",
       "         0.28689077, 0.23152627, 0.2777903 , 0.2436829 , 0.19449331,\n",
       "         0.20463906, 0.19973801, 0.20529924, 0.16101557, 0.15031043,\n",
       "         0.1422553 , 0.14006627, 0.15901694, 0.13028951, 0.11863053,\n",
       "         0.121484  , 0.10098063, 0.09035677, 0.11454533, 0.13028763,\n",
       "         0.10922962, 0.09657527, 0.08973779, 0.07784241, 0.07674973,\n",
       "         0.05712752, 0.03534822, 0.0352255 , 0.02417726, 0.01790764,\n",
       "         0.02126767, 0.0238901 , 0.02257771, 0.0179578 , 0.01722747,\n",
       "         0.01416573, 0.01779152, 0.02206193, 0.02553867, 0.02859218,\n",
       "         0.02915431, 0.02971742, 0.02302175, 0.01702518, 0.01855571,\n",
       "         0.02286824, 0.02350399, 0.0187223 , 0.02249457, 0.02306413,\n",
       "         0.01909094, 0.0188887 , 0.01487016, 0.01833394, 0.02253257,\n",
       "         0.02473382, 0.02479234, 0.02301646, 0.02273029, 0.01999848,\n",
       "         0.0219816 , 0.02008917, 0.01775548, 0.02734496, 0.02405925,\n",
       "         0.01458929, 0.02339312, 0.02311289, 0.01609114, 0.01888736,\n",
       "         0.01928776, 0.01579895, 0.01701297, 0.02094736, 0.02340566,\n",
       "         0.02108061, 0.01897741, 0.01861368, 0.01735231, 0.02104396,\n",
       "         0.02213419, 0.01831198, 0.02110504, 0.02393169, 0.03066067,\n",
       "         0.03098568, 0.02412639, 0.02946295, 0.04009214, 0.03741983,\n",
       "         0.02179487, 0.01390277, 0.0199807 , 0.02670095, 0.02510303,\n",
       "         0.01882618, 0.01629201, 0.01609613, 0.01510044, 0.01254703,\n",
       "         0.01100008, 0.01405657, 0.01499087, 0.01283835, 0.01430891,\n",
       "         0.02223014, 0.02379673, 0.01711785, 0.01947354, 0.02273256,\n",
       "         0.02301102, 0.02125987, 0.01874625, 0.02007555, 0.02207687,\n",
       "         0.01924629, 0.01744348, 0.02037104, 0.02129641, 0.0250054 ,\n",
       "         0.02675539, 0.02268187, 0.0197878 , 0.01873955, 0.01900908,\n",
       "         0.01777631, 0.01164313, 0.01421964, 0.01475013, 0.01528343,\n",
       "         0.020884  , 0.02357243]], dtype=float32))"
      ]
     },
     "execution_count": 248,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "template = librosa.load(r'./recordings/hei_1.wav', sr=None)\n",
    "# template = librosa.load(r'./template_processed_22.wav', sr=None)\n",
    "\n",
    "energies = librosa.feature.rms(y=template[0], frame_length=CHUNK, hop_length=CHUNK//2)\n",
    "# S, phase = librosa.magphase(librosa.stft(template[0]))\n",
    "# energies = librosa.feature.rms(S=S)\n",
    "\n",
    "energies.shape, energies"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 246,
   "id": "phantom-depth",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.045363735407590866\n",
      "(22528,)\n"
     ]
    }
   ],
   "source": [
    "thres = 2 * np.median(energies[0])\n",
    "print(thres)\n",
    "keep_index = np.where(energies[0] > thres)\n",
    "\n",
    "new_signal_index = librosa.frames_to_samples(keep_index, hop_length=256)[0]\n",
    "\n",
    "new_signal = np.concatenate([template[0][x: x + 256] for x in new_signal_index if x < len(template[0])])\n",
    "\n",
    "# segments = np.array(\n",
    "#     [template[0][x: x + segment_frames] for x in range(0, len(template[0]), segment_frames)]\n",
    "# )\n",
    "\n",
    "# new_signal = np.concatenate(segments[keep_index])\n",
    "# new_signal = np.zeros(template[0].shape)\n",
    "# new_signal[keep_index] = template[0][keep_index]\n",
    "print(new_signal.shape)\n",
    "new_signal *= 2**15  # AKA, new_signal *= 2<<14, this is for PyAudio to be able to play this file\n",
    "wavfile.write(\"./template_processed_22.wav\", 44100, new_signal.astype(np.int16))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 181,
   "id": "several-navigator",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[    0,   256,   512,  3328,  3584,  3840,  4096,  4352,  4608,\n",
       "         4864,  5120,  5376,  5632,  5888,  6144,  6400,  6656,  6912,\n",
       "         7168,  7424,  7680,  7936,  8192,  8448,  8704,  8960,  9216,\n",
       "         9472,  9728,  9984, 10240, 10496, 10752, 11008, 11264, 11520,\n",
       "        11776, 12032, 12288, 12544, 12800, 13312, 13568, 13824, 14080,\n",
       "        14336, 14592, 14848, 15104, 15360, 15616, 15872, 16128, 16384,\n",
       "        16640, 16896, 17152, 17408, 17664, 17920, 18176, 18432, 18688,\n",
       "        18944, 19200, 19456, 19712, 19968, 20224, 20480, 20736, 20992,\n",
       "        21248, 21504, 21760, 22016, 22272, 22528, 22784, 23040, 23296,\n",
       "        23552, 23808, 24064, 24320, 24576, 24832, 25088, 25344, 25600,\n",
       "        25856, 26112, 26368, 26624, 26880, 27136, 27392, 27648, 27904,\n",
       "        28160, 28416, 28672, 28928, 29184, 29440, 29696, 29952, 30208,\n",
       "        30464, 30720, 30976, 31232, 31488, 31744, 32000, 32256, 32512,\n",
       "        32768, 33024, 33280, 33536, 33792, 34048, 34304, 34560, 34816,\n",
       "        35072, 35328, 35584, 35840, 36096, 36352, 36608, 36864, 37120,\n",
       "        37376, 37632, 37888, 38144, 38400, 38656, 38912, 39168, 39424,\n",
       "        39680, 39936, 40192, 40448, 40704, 40960, 41216, 41472, 41728,\n",
       "        41984, 42240, 42496, 42752, 43008, 43264, 43520, 43776, 44032,\n",
       "        44288, 44544, 44800, 45056, 45312, 45568, 45824, 46080, 46336,\n",
       "        46592, 46848, 47104, 47360, 47616, 47872, 48128, 48384, 48640,\n",
       "        48896, 49152, 49408, 49664, 49920, 50176, 50432, 50688, 50944,\n",
       "        51200, 51456, 51712, 51968, 52224, 52480, 52736, 52992, 53248,\n",
       "        53504, 53760, 54016, 54272, 54528, 54784, 55040, 55296, 55552,\n",
       "        55808, 56064, 56320, 56576, 56832, 57088, 57344, 57600, 57856,\n",
       "        58112, 58368, 58624, 58880, 59136, 59392, 59648, 59904, 60160,\n",
       "        60416, 60672, 60928, 61184, 61440, 61696, 61952, 62208, 62464,\n",
       "        62720, 62976, 63232, 63488, 63744, 64000, 64256, 64512, 64768,\n",
       "        65024, 65280, 65536, 65792, 66048, 66304, 66560, 66816, 67072,\n",
       "        67328, 67584, 67840, 68096, 68352, 68608, 68864, 69120, 69376,\n",
       "        69632, 69888, 70144, 70656, 70912, 71168, 71424, 71680, 71936,\n",
       "        72192, 72448, 72704, 72960, 73216, 73472, 73728, 73984, 74240,\n",
       "        74496, 74752, 75008, 75264, 75520, 75776, 76032, 76288, 76544,\n",
       "        76800, 77056, 77312, 77568, 77824, 78080, 78336]])"
      ]
     },
     "execution_count": 181,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# template[0].shape, new_signal.shape\n",
    "new_signal_index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 196,
   "id": "close-exchange",
   "metadata": {},
   "outputs": [],
   "source": [
    "hei = librosa.load(r'./recordings/1_2.wav', sr=44100)\n",
    "zuoxia = librosa.load(r'./recordings/1_4.wav', sr=44100)\n",
    "\n",
    "wavfile.write(\"./hei_processed.wav\", 44100, strip_silence(hei[0]))\n",
    "wavfile.write(\"./zuoxia_processed.wav\", 44100, strip_silence(zuoxia[0]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "specialized-bedroom",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([], dtype=float32)"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "strip_silence(hei[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "final-field",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "moral-summary",
   "metadata": {},
   "outputs": [],
   "source": [
    "def strip(x, frame_length, hop_length):\n",
    "\n",
    "    # Compute RMSE.\n",
    "    rmse = librosa.feature.rmse(x, frame_length=frame_length, hop_length=hop_length, center=True)\n",
    "    \n",
    "    # Identify the first frame index where RMSE exceeds a threshold.\n",
    "    thresh = 0.5 * np.median(rmse)\n",
    "    frame_index = 0\n",
    "    while rmse[0][frame_index] < thresh:\n",
    "        frame_index += 1\n",
    "        \n",
    "    # Convert units of frames to samples.\n",
    "    start_sample_index = librosa.frames_to_samples(frame_index, hop_length=hop_length)\n",
    "    \n",
    "    # Return the trimmed signal.\n",
    "    return x[start_sample_index:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "reasonable-interaction",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "spanish-intent",
   "metadata": {},
   "source": [
    "# 端点检测-2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "id": "composite-buffalo",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1224, 64)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "((1224,),\n",
       " array([0.0002512 , 0.000174  , 0.00012401, ..., 0.00055665, 0.00082946,\n",
       "        0.00045964]))"
      ]
     },
     "execution_count": 129,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "segment_frames = 64\n",
    "template = librosa.load(r'./recordings/1_1.wav', sr=None)\n",
    "wave_data = template[0]\n",
    "\n",
    "segments = np.array(\n",
    "    [template[0][x: x + segment_frames] for x in range(0, len(template[0]), segment_frames)]\n",
    ")\n",
    "print(segments.shape)\n",
    "# energies = librosa.feature.rms(y=template[0], frame_length=segment_frames, hop_length=segment_frames)\n",
    "# S, phase = librosa.magphase(librosa.stft(template[0]))\n",
    "# energies = librosa.feature.rms(S=S)\n",
    "energies = np.array([(s**2).sum() / len(s) for s in segments])\n",
    "\n",
    "energies.shape, energies"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "id": "interested-charm",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.0015109843807294965\n",
      "(23808,)\n"
     ]
    }
   ],
   "source": [
    "thres = 3 * np.median(energies)\n",
    "\n",
    "print(thres)\n",
    "keep_index = (np.where(energies > thres)[0])\n",
    "new_signal = np.concatenate(segments[keep_index])\n",
    "# new_signal = np.zeros(template[0].shape)\n",
    "# new_signal[keep_index] = template[0][keep_index]\n",
    "print(new_signal.shape)\n",
    "wavfile.write(\"./template_processed_3.wav\", 44100, new_signal)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "color-season",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "specified-heading",
   "metadata": {},
   "source": [
    "# 匹配"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 252,
   "id": "optical-clothing",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 252,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'asd.dd'.endswith('.DD')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "weird-primary",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "billion-frequency",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "broke-shareware",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "awful-spencer",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "agreed-relation",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "negative-transfer",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "elementary-classification",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "abstract-failing",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:petoi]",
   "language": "python",
   "name": "conda-env-petoi-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
